<?php
namespace app\api\controller\pay;

use app\api\controller\AuthBase;
use app\common\business\Pay as payBusi;
use app\common\lib\Show;
use think\annotation\Route;
use think\facade\Log;
use app\common\business\Order as orderBusi;
// 支付API
class Index extends AuthBase 
{
	/**
	 * @Route("unifiedOrder",method="POST")
	 * 订单支付
	*/
	public function unifiedOrder(){
		//获取提交参数
		$params = input('param.');
		//判断订单是否存在
		$res = (new orderBusi())->getOrderInfoById($params['order_id']);
		if(empty($res)){
			return Show::error('该订单不存在');
		}else{
			//return Show::success($res);
		}
		try{
			$result = (new payBusi())->unifiedOrder(
				$this->appId,
				config('pay.pay_types.weixin'),
				$params
			);
		}catch(\Exception $e){
			//记录日志
			Log::write(date('Y-m-d H:i:s',time()).$e->getMessage());
			return Show::error($e->getMessage());
		}
		if(!$result){
			return Show::error('支付失败');
		}
		return Show::success($result);
	}

	
	/**
	 * @Route("getOrder",method="POST")
	 * 支付订单查询
	*/
	public function getOrder(){
		$orderId = input('param.order_id','','trim');
		/*
		if(!$orderId){
			return Show::error('订单id错误');
		}
		*/
		
		try{
			$result = (new payBusi())->getOrder($orderId,$this->appId);
		}catch(\Exception $e){
			//记录日志
			Log::write(date('Y-m-d H:i:s',time()).$e->getMessage());
			return Show::error($e->getMessage());
		}

		return Show::success($result);
	}
}